#ifndef __HTTP_CONNECTION_H__
#define __HTTP_CONNECTION_H__


#include <boost/beast/core.hpp>
#include <boost/beast/http.hpp>
#include <boost/asio.hpp>

#include <boost/beast/http/dynamic_body.hpp>
#include <memory>
#include <cstdint>


const uint32_t MAX_LENGTH = 8192;
const uint16_t MAX_TIME = 60;

using Tcp = boost::asio::ip::tcp;

//记录请求次数
uint64_t request_count();
//返回当前时间戳
std::time_t now();


class HttpConnection: public std::enable_shared_from_this<HttpConnection>
{
public:
    HttpConnection(Tcp::socket socket)
    :_socket(std::move(socket))
    {

    };

    void start();
private:
    //读取请求
    void read_request();

    //是否超时
    void check_deadline();

    //处理请求
    void process_request();

    //创建响应
    void create_response();

private:
    Tcp::socket _socket;
    boost::beast::flat_buffer _buffer{MAX_LENGTH};
    boost::beast::http::request<boost::beast::http::dynamic_body> _request;
    boost::beast::http::response<boost::beast::http::dynamic_body> _response;
    std::shared_ptr<std::string> _doc_root;
    boost::asio::steady_timer _deadline{ //每条连接的最大处理时间
        _socket.get_executor(), std::chrono::seconds(MAX_TIME)
    }; 

};



#endif // !__HTTP_CONNECTION_H__